package com.mofei.h003test.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.mofei.h003test.R;
import com.mofei.h003test.presenter.ControlActivityPresenter;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

/**
 * @author : timmy
 * @date : 2018/9/20
 */
public class ControlActivity extends RxAppCompatActivity {

    public static final String EXTRA_MAC_ADDRESS = "extra_mac_address";
    private Button mBtnConnect;
    private TextView mTvTitle;
    private TextView mTvSalveAddress;
    private ControlActivityPresenter mPresenter;

    public static void start(Context context, String macAddress) {
        Intent intent = new Intent(context, ControlActivity.class);
        intent.putExtra(EXTRA_MAC_ADDRESS, macAddress);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_control);

        initView();
        initData();
    }

    private void initView() {
        mBtnConnect = findViewById(R.id.connect);
        mTvTitle = findViewById(R.id.title);
        mTvSalveAddress = findViewById(R.id.salveaddress);
        mBtnConnect.setOnClickListener(mOnClickListener);
        findViewById(R.id.back).setOnClickListener(mOnClickListener);
        findViewById(R.id.getslaveaddress).setOnClickListener(mOnClickListener);
        findViewById(R.id.start_shock).setOnClickListener(mOnClickListener);
        findViewById(R.id.stop_shock).setOnClickListener(mOnClickListener);
        findViewById(R.id.shut_down).setOnClickListener(mOnClickListener);
        findViewById(R.id.unbind).setOnClickListener(mOnClickListener);
        findViewById(R.id.bind).setOnClickListener(mOnClickListener);
        findViewById(R.id.connect_salve).setOnClickListener(mOnClickListener);
    }

    private void initData() {
        String address = getIntent().getStringExtra(EXTRA_MAC_ADDRESS);
        mPresenter = new ControlActivityPresenter(this, address);
    }

    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int id = view.getId();
            switch (view.getId()) {
                case R.id.connect:
                    if (!mPresenter.isConnected()) {
                        // 连接设备
                        mPresenter.startConnectDevice();
                    } else {
                        mPresenter.triggerDisconnect();
                    }
                    break;
                case R.id.back:
                    finish();
                    break;
                case R.id.getslaveaddress:
                    byte[] getslaveaddress = {(byte) 0xBB, (byte) 0x91, (byte) 0x0f, (byte) 0x00, (byte) 0x00, (byte) 0x82};
                    mPresenter.registerWrite(getslaveaddress);
                    break;
                case R.id.start_shock:
                    byte[] startShock = {(byte) 0xBB, (byte) 0x67, (byte) 0x00, (byte) 0x01, (byte) 0xE4, (byte) 0x82};
                    mPresenter.registerWrite(startShock);
                    break;
                case R.id.stop_shock:
                    byte[] stopShock = {(byte) 0xBB, (byte) 0x83, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x82};
                    mPresenter.registerWrite(stopShock);
                    break;
                case R.id.shut_down:
                    byte[] shutDown = {(byte) 0xBB, (byte) 0x8F, (byte) 0x0D, (byte) 0x00, (byte) 0x00, (byte) 0x82};
                    mPresenter.registerWrite(shutDown);
                    break;
                case R.id.bind:
                    byte[] bind = {(byte) 0xBB, (byte) 0x8C, (byte) 0x0A, (byte) 0x00, (byte) 0x00, (byte) 0x82};
                    mPresenter.registerWrite(bind);
                    break;
                case R.id.unbind:
                    byte[] unbind = {(byte) 0xBB, (byte) 0x8D, (byte) 0x0B, (byte) 0x00, (byte) 0x00, (byte) 0x82};
                    mPresenter.registerWrite(unbind);
                    break;
                case R.id.connect_salve:
                    byte[] connectSalve = {(byte) 0xBB, (byte) 0x8B, (byte) 0x09, (byte) 0x00, (byte) 0x00, (byte) 0x82};
                    mPresenter.registerWrite(connectSalve);
                    break;
                default:
                    break;
            }
        }
    };

    public void showToast(final String throwable) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(ControlActivity.this, "" + throwable, Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void connectState(int state) {
        switch (state) {
            case 1:
                mTvTitle.setText("设备连接中..");
                break;
            case 2:
                mTvTitle.setText("设备连接成功.");
                mBtnConnect.setText("断开连接");
                break;
            case 3:
                mTvTitle.setText("设备已经断开连接.");
                mBtnConnect.setText("连接设备");
                mTvSalveAddress.setText("从机地址: null");
                break;
            default:
                break;
        }
    }

    public void setSalveAddress(String data) {
        //data = new StringBuffer(data).reverse().toString().substring(0, data.length() - 4);

        mTvSalveAddress.setText("从机地址: " + data);
    }
}
